package com.ruoyi.system.apiController;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.Account;
import com.ruoyi.system.domain.YyAdmin;
import com.ruoyi.system.domain.YyDoctor;
import com.ruoyi.system.domain.YyPatient;
import com.ruoyi.system.enums.RoleEnum;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.IYyAdminService;
import com.ruoyi.system.service.IYyDoctorService;
import com.ruoyi.system.service.IYyPatientService;
import com.ruoyi.system.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/patient")
public class YyPatientApiController {

    @Autowired
    private IYyPatientService yyPatientService;
    @Autowired
    private IYyDoctorService yyDoctorService;
    @Autowired
    private IYyAdminService yyAdminService;


    /**
     * 患者注册
     */
    @PostMapping("/register")
    public AjaxResult register(YyPatient yyPatient) {
        int result = yyPatientService.insertYyPatient(yyPatient);
        if (result > 0){
            return AjaxResult.success("注册成功");
        } else if (result == -1) {
            return AjaxResult.error("手机号已被注册");
        } else if (result == -2) {
            return AjaxResult.error("身份证号错误，请输入正确的身份证号码");
        }
        return AjaxResult.error("注册失败");
    }

    /**
     * 查询患者
     */
    @GetMapping("/getPatient")
    public AjaxResult getPatient(Integer id) {
        if (ObjectUtil.isNull(id)){
            List<YyPatient> yyPatients = yyPatientService.selectYyPatientList(new YyPatient());
            return AjaxResult.success(yyPatients);
        }else{
            YyPatient yyPatient = yyPatientService.selectYyPatientByPatientId(Long.parseLong(String.valueOf(id)));
            return AjaxResult.success(yyPatient);
        }
    }

    /**
     * 患者查看自己的预约信息
     */
    @GetMapping("/appointment")
    public AjaxResult appointment(Integer id) {
        List<Map<String,Object>> yyPatient = yyPatientService.selectByPatientId(Long.parseLong(String.valueOf(id)));
        return AjaxResult.success(yyPatient);
    }

    /**
     * 删除患者
     */
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        int result = yyPatientService.deleteYyPatientByPatientId(id);
        if (result > 0){
            return AjaxResult.success("删除成功");
        }
        return AjaxResult.error("删除失败");
    }



}
